<template>
  <div class="page-container">
    <Position :menu-code="menuInfo.categoryId" :picture="rootMenuInfo.picture" :name="menuInfo.title" class="position"></Position>
    <div class="cus-container">
      <!-- B部分：详细内容区域 -->
      <div class="product-content-section mt-12 lg:mt-16">
        <div class="space-y-8">
          <!-- 富文本内容 -->
          <div v-if="menuInfo.content" class="content-block">
            <h2 class="text-xl text-align-center lg:text-2xl font-bold text-gray-900 mb-4">{{ menuInfo.title }}</h2>
            
            <!-- 文章元信息 -->
            <div class="article-meta mb-6 pb-4 border-b border-gray-200">
              <div class="flex flex-wrap items-center gap-4 text-sm text-gray-500">
                <div class="flex items-center gap-1">
                  <Icon name="i-heroicons-calendar-days" class="w-4 h-4" />
                  <span>{{ formateDate(menuInfo.updatedAt || menuInfo.createdAt, 'YYYY-MM-DD') }}</span>
                </div>
                <div v-if="menuInfo.author" class="flex items-center gap-1">
                  <Icon name="i-heroicons-user" class="w-4 h-4" />
                  <span>{{ menuInfo.author }}</span>
                </div>
                <div class="flex items-center gap-1">
                  <Icon name="i-heroicons-eye" class="w-4 h-4" />
                  <span>{{ menuInfo.viewCount || 0 }} 次阅读</span>
                </div>
              </div>
            </div>
            
            <div class="rich-content prose prose-lg max-w-none" v-html="menuInfo.content"></div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { getDetailByLinkName } from "@/api/projects";
import Position from "~/components/Position.vue";
const route = useRoute();
const { id } = route.params;
const menuInfo = ref<any>({});
const rootMenuInfo = ref<any>({});
const appConfig = useAppConfig();
const { menus, companyInfo } = appConfig.systemConfig; // 菜单
// 轮播图相关状态
const carousel = useTemplateRef("carousel");
const activeIndex = ref(0);
const thumbnailImages = ref<any[]>([]);

/**
 * 初始化页面数据
 * 获取产品详情并查找对应的根节点菜单信息
 */
const initPage = async () => {
  try {
    const detail = await getDetailByLinkName(id as string);
    // 根据产品的categoryId查找对应的根节点菜单
    const rootMenu = useFindRootMenuByCode(menus, detail.categoryId);
    if (rootMenu) {
      rootMenuInfo.value = rootMenu;
    }
    if (detail) {
      menuInfo.value = detail;

      // 初始化图片数据
      initializeImages(detail);

      useHead({
        title: menuInfo.value.title,
        meta: [
          { name: "keywords", content: menuInfo.value.seoKey },
          { name: "description", content: menuInfo.value.description }
        ]
      });
    }
    console.log(detail, "产品详情");
  } catch (error) {
    console.error("初始化页面失败:", error);
  }
};

/**
 * 初始化图片数据
 * 合并mainImages和thumbnailImages
 */
const initializeImages = (detail: any) => {
  const images = [];

  // 添加主图
  if (detail.mainImages && detail.mainImages.length > 0) {
    images.push(...detail.mainImages);
  }

  // 添加缩略图（去重）
  if (detail.thumbnailImages && detail.thumbnailImages.length > 0) {
    detail.thumbnailImages.forEach((thumb: any) => {
      const imageUrl = thumb.url || thumb;
      if (!images.includes(imageUrl)) {
        images.push(thumb);
      }
    });
  }

  thumbnailImages.value = images;
};

initPage();
</script>

<style scoped lang="scss">
.page-container {
  min-height: 100vh;
  background: #f8fafc;
}

.btn-primary {
  background: $custom-primary;
  color: white;
  border: none;
  display: inline-flex;
  align-items: center;
}

.btn-primary:hover {
  background: color-mix(in srgb, $custom-primary 90%, black);
}

.btn-secondary {
  background: transparent;
  color: $custom-primary;
  border: 2px solid $custom-primary;
  display: inline-flex;
  align-items: center;
}

.btn-secondary:hover {
  background: $custom-primary;
  color: white;
}

.content-block {
  background: white;
  padding: 2rem;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.rich-content {
  line-height: 1.8;
}

.rich-content table {
  width: 100%;
  border-collapse: collapse;
  margin: 1rem 0;
}

.rich-content table th,
.rich-content table td {
  border: 1px solid #e5e7eb;
  padding: 0.75rem;
  text-align: left;
}

.rich-content table th {
  background-color: #f9fafb;
  font-weight: 600;
}

.rich-content p {
  margin-bottom: 1rem;
}

.rich-content strong {
  font-weight: 600;
  color: #1f2937;
}

/* 响应式布局 */
@media (max-width: 1024px) {
  .product-main-section {
    padding: 1rem 0;
  }

  .content-block {
    padding: 1.5rem;
  }
}

@media (max-width: 768px) {
  .thumbnail-list .flex {
    justify-content: center;
  }

  .action-buttons .flex {
    flex-direction: column;
  }

  .action-buttons button {
    width: 100%;
    justify-content: center;
  }
}
</style>
